define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index : function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pms/pmsorderinfo/index' + location.search,
                    table    : 'pms_order_info'
                }
            });

            var table = $('#table');

            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $('form', table.$commonsearch);
                $('input[name=\'room_type\']', form).addClass('selectpage').data('source', 'pms/pmsroomtype/selectpage');
                $('input[name=\'source\']', form).addClass('selectpage').data('source', 'pms/pmsconfig/selectpage').data('primaryKey', 'id').data('field', 'name').attr('data-params', '{"key":"source"}');
                $('input[name=\'room_info_id\']', form).addClass('selectpage').data('source', 'pms/pmsroominfo/selectpage').data('primaryKey', 'id').data('field', 'room_no');

                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url              : $.fn.bootstrapTable.defaults.extend.index_url,
                pk               : 'id',
                sortName         : 'id',
                searchFormVisible: true,
                fixedColumns     : true,
                fixedRightNumber : 1,
                columns          : [
                    [
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'reserve_info', title: __('预定订单号'), operate: 'LIKE', visible: false},
                        {
                            field: 'order_no', title: __('Order_no'), operate: 'LIKE', formatter: function (value, row, index) {
                                var txt = value;
                                if (row['reserve_no'] == '' || typeof row['reserve_no'] == 'undefined' || row['reserve_no'] == null) {
                                    txt = value;
                                } else {
                                    txt =
                                        value + '<br /><span class="label label-success">预定号:' + row['reserve_no'] + '</span>'
                                }
                                return txt;
                            }
                        },
                        {field: 'source', title: __('Source')},
                        {
                            field     : 'realname', title: __('联系人'), formatter: function (value, row, index) {
                                var sex_name = '女孩';
                                var sex_class = 'label-danger';
                                if (row['sex'] == 1) {
                                    sex_name = '男孩';
                                    sex_class = 'label-info';
                                }
                                return row['realname'] + ' <label class="label ' + sex_class + '">' + sex_name + '</label>';
                            }, operate: 'LIKE'
                        },
                        {field: 'tel', title: __('Tel'), operate: 'LIKE'},
                        {field: 'check_in_time', title: __('Check_in_time'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'check_out_time', title: __('Check_out_time'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'days', title: __('Days'), operate: 'BETWEEN'},
                        {
                            field: 'room_info_id', title: __('床号'), formatter: function (value, row, index) {
                                return '<span class="label label-primary">' + row['room_type_name'] + '</span><br />' + row['room_no'] + '床';
                            }
                        },
                        {
                            field: 'order_status', title: __('Order_status'), formatter: function (value, row, index) {
                                switch (value) {
                                    case -1:
                                        return '<span style="color:#3a7af3">已受理</span>';
                                    case 0:
                                        return '<span style="color:green">已入住</span>';
                                    case 1:
                                        return '<span style="color:#949494">已出院</span>';
                                    case 2:
                                        return '<span style="color:red">作废</span>';
                                }
                            }
                        },

                        {field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'last_editor_nickname', title: __('Last_editor'), operate: false},
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons: [
                                {
                                    name     : '订单详情',
                                    title    : __('订单详情'),
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    text     : '订单详情',
                                    icon     : 'fa fa-file-text-o',
                                    extend   : 'data-area=\'["80%","80%"]\'',
                                    url      : 'pms/pmsorderinfo/detail'
                                }
                            ]
                        }
                    ]
                ]
            });

            //双击编辑
            $(table).on('dblclick', 'tr', function (e) {
                e.preventDefault();
                $(this).find('.btn-dialog').click();
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add   : function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
            Form.api.bindevent($('form[role=modalform]'));

            setTimeout(function () {
                $('.plupload-preview a.btn-trash').remove();
            }, 50);

            $('#js_operate-box button').on('click', function () {
                var that  = $(this);
                var event = that.data('event');
                $('#editModal #m-event').val(event);
                switch (event) {
                    case 'edit-linkman':
                        $('#editModal').modal('show');
                        $('#editModal #editModalLabel').text('改患者信息');
                        $('#m-edit-linkman').show();
                        $('#m-edit-date').hide();
                        $('#m-edit-room').hide();
                        break;
                    case 'edit-room':
                        $('#editModal').modal('show');
                        $('#editModal #editModalLabel').text('更换床位');
                        $('#m-edit-linkman').hide();
                        $('#m-edit-date').hide();
                        $('#m-edit-room').show();
                        break;
                    case 'edit-date':
                        $('#editModal').modal('show');
                        $('#editModal #editModalLabel').text('改住院信息');
                        $('#m-edit-linkman').hide();
                        $('#m-edit-date').show();
                        $('#m-edit-room').hide();
                        break;
                }
            });

            $('#cancel_reservation').click(function () {
                console.log(2);
                Layer.confirm('此操作不可撤销，请谨慎操作！', {
                    title: "您确认取消此预约吗",
                    btn  : ['确认', '取消'] //按钮
                }, function () {
                    Backend.api.ajax({
                        url   : 'pms/Pmsorderinfo/cancel',
                        data  : {id: $('#c-id').val()},
                        method: 'POST'
                    }, function (d) {
                        Layer.msg('取消成功', function () {
                            location.reload();
                        });
                    });
                    return true;
                });

            });
            $('#editModal .btn-primary').on('click', function (e) {
                var that    = $(this);
                var event   = $('#editModal #m-event').val();
                var remarks = $('#c-cancelRemarks').val();

                var params                   = {};
                params['id']                 = $('#c-id').val();
                params['event']              = event;
                params['disease_id']         = $('#m-disease_id').val();
                params['sex']                = $('#m-sex').val();
                params['realname']           = $('#m-realname').val();
                params['certificates_type']  = $('#m-certificates_type').val();
                params['person_no']          = $('#m-person_no').val();
                params['birthday']           = $('#m-birthday').val();
                params['nation']             = $('#m-nation').val();
                params['tel']                = $('#m-tel').val();
                params['order_remarks']      = $('#m-order_remarks').val();
                params['check_in_time']      = $('#m-check_in_time').val();
                params['check_out_time']     = $('#m-check_out_time').val();
                params['days']               = $('#m-days').val();
                params['order_invalid_memo'] = remarks;
                params['customer_data']      = $('#m-customer_data').val();

                if (event === 'edit-room') {
                    let roomInfo = $('#m-room-info').val().split(',');
                    if (roomInfo.length < 2) {
                        return Layer.msg('请选择');
                    }
                    params['room_info_id']   = roomInfo[0];
                    params['room_type_id']   = roomInfo[1];
                    params['room_info_text'] = $.trim($('#m-room-info option:selected').text());
                }

                Backend.api.ajax({
                    url : 'pms/Pmsroomstatus/ajax_update_order_info',
                    data: params
                }, function (d) {
                    $('#editModal').modal('hide');
                    window.location.reload();
                });
            });
        },
        api   : {
            bindevent: function () {
                Form.api.bindevent($('form[role=form]'));
            }
        }
    };
    return Controller;
});